<?php
/**
 * Name: SmsTemplateController.class.php
 * Description:
 * User: DIY
 * Date: 2016/9/24
 * Time: 15:44
 */

namespace Admin\Controller;


use Admin\Library\BootstrapPage;
use Common\Logic\SmsLogic;
use Common\Model\SmsTemplateModel;

class SmsTemplateController extends BaseController
{

    /*
     * 模版列表
     * */
    public function index(){
        //搜索数据
        $data = array(
            '1'=>'1',
        );
        if(I('get.title')){
            $data['title'] = array('like','%'.I('get.title').'%');
        }
        if(I('get.type')!=''){
            $data['type'] = array('like','%'.I('get.type').'%');
        }
        $SmsTemplateModel = new SmsTemplateModel();
        $count = $SmsTemplateModel->where($data)->count();
        $Page = new BootstrapPage($count,20);
        $list['page'] = $Page->show();
        $list['list'] = $SmsTemplateModel->where($data)->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);
        $this->display();
    }

    /*
     * 添加模版
     * */
    public function add(){
        if(IS_POST){
            $SmsTemplateModel = new SmsTemplateModel();
            if($SmsTemplateModel->create()){
                if($SmsTemplateModel->add()){
                    $this->success('添加成功',U('Admin/SmsTemplate/index'));
                }
                else{
                    $this->error('添加失败：'.$SmsTemplateModel->getDbError());
                }
            }
            else{
                $this->error('添加失败：'.$SmsTemplateModel->getError());
            }
        }
        else{
            $this->display();
        }
    }

    /*
     * 编辑模版
     * */
    public function edit($id){
        $SmsTemplateModel = new SmsTemplateModel();
        if(IS_POST){
            if($SmsTemplateModel->create()){
                if($SmsTemplateModel->save()){
                    $this->success('更新成功',U('Admin/SmsTemplate/index'));
                }
                else{
                    $this->error('更新失败：'.$SmsTemplateModel->getDbError());
                }
            }
            else{
                $this->error('更新失败：'.$SmsTemplateModel->getError());
            }
        }
        else{
            $tmp = $SmsTemplateModel->find($id);
            $this->assign('tmp',$tmp);
            $this->display();
        }
    }

    /*
     * 删除模版
     * */
    public function del($id){
        $SmsTemplateModel = new SmsTemplateModel();
        $SmsTemplateModel->delete($id);
        $this->success('删除成功',U('Admin/AdminRole/index'));
    }

    /*
     *
     * 查看模版
     * */
    public function show($id){
        $SmsTemplateModel = new SmsTemplateModel();
        $tmp = $SmsTemplateModel->find($id);
        $this->assign('tmp',$tmp);
        $this->display();
    }
}